package com.killgun.mainplayer;

import com.jme3.app.Application;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.bullet.objects.PhysicsCharacter;
import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.killgun.MainApp;
import com.killgun.physics.PhysicsAppState;

/**
 *
 * @author Austin Allman
 */
public class MainPlayerAppState extends AbstractAppState
        implements ActionListener {

    private PhysicsCharacter player;
    private Camera cam;
    private boolean left = false, right = false, up = false, down = false;
    private Vector3f walkDirection = new Vector3f();
    private MainApp main;

    @Override
    public void initialize(AppStateManager stateManager, Application app) {
        super.initialize(stateManager, app);
        main = (MainApp) app;
        cam = main.getCamera();
        main.getFlyByCamera().setMoveSpeed(100f);

    }

    /*
     * TO-DO
     * Move this stuff out into the MainPlayerActionListener class
     * 
     */
    public void onAction(String binding, boolean value, float tpf) {
        // reworked this, otherwise possible to look down and fly vertically..
        if (binding.equals("Lefts")) {
            left = value;

        } else if (binding.equals("Rights")) {
            right = value;

        } else if (binding.equals("Ups")) {
            up = value;

        } else if (binding.equals("Downs")) {
            down = value;

        } else if (binding.equals("Space")) {
            if (value) {
                player.jump();
            }
        }
    }

    @Override
    public void update(float tpf) {
        super.update(tpf);
        if (player == null) {
            player = main.getStateManager().getState(PhysicsAppState.class).addPlayer();
            setupKeys();
        } else {

            Vector3f camDir = cam.getDirection().clone().multLocal(0.6f);
            Vector3f camLeft = cam.getLeft().clone().multLocal(0.4f);
            walkDirection.set(0, 0, 0);
            if (left) {
                walkDirection.addLocal(camLeft);
            }
            if (right) {
                walkDirection.addLocal(camLeft.negate());
            }
            if (up) {
                walkDirection.addLocal(camDir);
            }
            if (down) {
                walkDirection.addLocal(camDir.negate());
            }
            player.setWalkDirection(walkDirection);
            cam.setLocation(player.getPhysicsLocation());
        }
    }

    private void setupKeys() {
        InputManager inputManager = main.getInputManager();
        inputManager.addMapping("Lefts", new KeyTrigger(KeyInput.KEY_A));
        inputManager.addMapping("Rights", new KeyTrigger(KeyInput.KEY_D));
        inputManager.addMapping("Ups", new KeyTrigger(KeyInput.KEY_W));
        inputManager.addMapping("Downs", new KeyTrigger(KeyInput.KEY_S));
        inputManager.addMapping("Space", new KeyTrigger(KeyInput.KEY_SPACE));
        inputManager.addListener(this,
                "Lefts", "Rights", "Ups", "Downs", "Space");
    }
}